package com.weimengchao.task.tool;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.UUID;

/**
 * @author weimc
 * @date 2019/6/11
 * @description:
 */
public class FileTool {

    private final static Logger logger = LoggerFactory.getLogger(FileTool.class);


    /**
     * 核对后缀 以".xls" ".xlsx" ".csv"结尾返回true
     *
     * @param excelName 文件名
     * @return true:核对成功,false:核对失败
     */
    public static boolean checkExcelSuffix(String excelName) {
        excelName = excelName.toLowerCase();
        if (excelName.contains(" ")) {
            return false;
        } else if (excelName.endsWith(".xls")) {
            return true;
        } else if (excelName.endsWith(".xlsx")) {
            return true;
        } else if (excelName.endsWith(".csv")) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 核对图片大小,格式
     * BMP（位图）、JPG、JPEG、PNG、GIF
     *
     * @param fileName 文件名
     * @param size     文件大小
     * @return true:核对成功,false:核对失败
     */
    public static boolean checkImg(String fileName, long size) {
        if (size > 10 * 1024 * 1024) {
            return false;
        }
        fileName = fileName.toLowerCase();
        if (fileName.endsWith(".bmp")) {
            return true;
        } else if (fileName.endsWith(".jpg")) {
            return true;
        } else if (fileName.endsWith(".jpeg")) {
            return true;
        } else if (fileName.endsWith(".png")) {
            return true;
        } else if (fileName.endsWith(".gif")) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 获得新的文件名: 32位UUID+文件名后缀
     *
     * @param fileName
     * @return
     */
    public static String getNewFileName(String fileName) {
        String fileNameSuffix = fileName.substring(fileName.lastIndexOf("."));
        return new StringBuffer(getUUIDFor32()).append(fileNameSuffix).toString();
    }

    /**
     * 获得32位UUID
     *
     * @return
     */
    public static String getUUIDFor32() {
        String uuid = UUID.randomUUID().toString();
        return uuid.replaceAll("-", "");
    }

    /**
     * 获取文件后缀
     *
     * @param fileName
     * @return
     */
    public static String getSuffix(String fileName) {
        return fileName.substring(fileName.lastIndexOf("."));
    }


    /**
     * 获取图片长度异常
     *
     * @param link 图片链接
     * @return 图片大小单位：字节
     */
    public static long getUrlLength(String link) {
        try {
            URL url = new URL(link);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            long len = urlConnection.getContentLength();
            if (len > 0) {
                return len;
            } else {
                logger.warn("[getUrlLength异常]无法获知文件大小;link:{}", link);
                return 0;
            }
        } catch (MalformedURLException e) {
            logger.error("[getUrlLength异常]link:{}, {}", link, e);
        } catch (IOException e) {
            logger.error("[getUrlLength异常]link:{}, {}", link, e);
        }
        return 0;
    }

}
